//
//  SQHomeworkLinkViewController.m
//  energy
//
//  Created by 周曼迪 on 2021/7/15.
//

#import "SQHomeworkLinkViewController.h"

#import "SQHomeworkLinkModel.h"

#import "CrazyFunction.h"

@interface SQHomeworkLinkViewController ()<BaseTableVCInterface>
@property (strong, nonatomic) NSMutableArray *dataArrayM;
@property (strong, nonatomic) NSMutableArray *dataArrayMID;
@end

@implementation SQHomeworkLinkViewController

-(NSMutableArray *)dataArrayM{
    if (!_dataArrayM) {
        _dataArrayM = NSMutableArray.new;
    }
    return _dataArrayM;
}

-(NSMutableArray *)dataArrayMID{
    if (!_dataArrayMID) {
        _dataArrayMID = NSMutableArray.new;
    }
    return _dataArrayMID;
}


- (void)viewDidLoad {
    self.tableViewInterface = self;
    [super viewDidLoad];
    self.navigationItem.title = @"作业环节";
    [self getHomeworkLinkData];
    
    LC_WEAK_SELF
    self.cellEvent = ^(int key) {
        SQHomeworkLinkModelData * dataModel = weakSelf.dataSouece[key];
        
        if ([weakSelf.dataArrayMID containsObject:dataModel.ipm_id]){
            [weakSelf.dataArrayMID removeObject:dataModel.ipm_id];
            dataModel.picStr = @"0";
            [weakSelf.dataArrayM removeObject:dataModel];
        }else{
            [weakSelf.dataArrayMID addObject:dataModel.ipm_id];
            dataModel.picStr = @"1";
            [weakSelf.dataArrayM addObject:dataModel];
        }
        [weakSelf reloadTableView];
    };
    
}

-(void)getHomeworkLinkData{
    [SQHomeworkLinkModel getDataWithParams:@{@"ipm_unit_two":self.ipm_unit_two,@"ipm_unit_three":self.ipm_unit_three,@"sa_enterprise":MEnterpriseId} WithCallBack:^(SQHomeworkLinkModel * data) {

        for (SQHomeworkLinkModelData * danger in data.data) {
            for (SQHomeworkLinkModelData *dic in self.accidentTypeIDs) {
                if ([danger.ipm_id isEqualToString:dic.ipm_id]) {
                    [self.dataArrayMID addObject:danger.ipm_id];
                    
                    [self.dataArrayM addObject:danger];
                    danger.picStr = @"1";
                    break;
                }
            }
            if (![danger.picStr isEqualToString:@"1"]) {
                danger.picStr = @"0";
            }
        }
        [self detailModelwithArr:data.data withCallBack:nil];
        [self reloadTableView];
        
    }];
}


-(NSInteger)rowsCount:(NSInteger)section{
    return self.dataSouece.count;
}

-(BaseModel *)modelForCell:(NSIndexPath *)indexPath{
    return self.dataSouece[indexPath.row];
}


- (NSArray<NSArray<NSString *> *> *)classArr{
    return @[@[@"SQHomeworkLinkTableViewCell"]];
}

- (IBAction)clickConfirm:(UIButton *)sender {
    NSMutableArray * arrayM = NSMutableArray.new;
    NSMutableArray * arrayMTitle = NSMutableArray.new;
    NSMutableArray * arrayMID = NSMutableArray.new;
    for (SQHomeworkLinkModelData * modelData in self.dataArrayM) {
        NSDictionary * dic = @{@"sal_operation_link_id":modelData.ipm_id,@"sal_operation_link_text":modelData.ipm_name};
        
        [arrayM addObject:dic];
        [arrayMID addObject:modelData];
        [arrayMTitle addObject:modelData.ipm_name];
    }
    
    NSString * josn = [CrazyFunction crazy_ObjectToJsonString:arrayM];
    NSString *string = [arrayMTitle componentsJoinedByString:@","];
    
    self.homeworkLinkBack(string,josn,arrayMID);
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
